//
//  SDBaseProgressView.m
//  SDProgressView
//
//  Created by aier on 15-2-19.
//  Copyright (c) 2015年 GSD. All rights reserved.
//  等待医生来电进度条

#import "SDBaseProgressView.h"

@implementation SDBaseProgressView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (progress <= 1.0) {
            [self setNeedsDisplay];
        }
    });
    
}

//-(void)setProgressStr:(NSString *)progressStr
//{
//    if (!_progressStr) {
//        self.progressStr = progressStr;
//    }
//}

- (void)setCenterProgressText:(NSString *)text withAttributes:(NSDictionary *)attributes
{
    CGFloat xCenter = self.frame.size.width * 0.5;
    CGFloat yCenter = self.frame.size.height * 0.5;
    
    // 判断系统版本
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        CGSize strSize = [text sizeWithAttributes:attributes];
        CGFloat strX = xCenter - strSize.width * 0.5;
        CGFloat strY = yCenter - strSize.height * 0.5;
        [text drawAtPoint:CGPointMake(strX, strY) withAttributes:attributes];
    }
        //else {
//        CGSize strSize;
//        NSAttributedString *attrStr = nil;
//        if (attributes[NSFontAttributeName]) {
////            strSize = [text sizeWithFont:attributes[NSFontAttributeName]];
//            strSize = [text sizeWithAttributes:attributes];
//            attrStr = [[NSAttributedString alloc] initWithString:text attributes:attributes];
//        } else {
//            strSize = [text sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
//            attrStr = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]}];
//        }
//        
//        CGFloat strX = xCenter - strSize.width * 0.5;
//        CGFloat strY = yCenter - strSize.height * 0.5;
//        
//        [attrStr drawAtPoint:CGPointMake(strX, strY)];
//    }
    
    
    
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGFloat xCenter = rect.size.width * 0.5;
    CGFloat yCenter = rect.size.height * 0.5;
    CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - progressMargin;
    
    
    //底部圆
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(xCenter, yCenter) radius:radius startAngle:- M_PI_2 endAngle:- M_PI_2 + M_PI*2 clockwise:YES];
    CGContextSetLineWidth(ctx, self.lineWidth);
    [_bgColor setStroke];//设置描边颜色
    CGContextAddPath(ctx, path1.CGPath);
    CGContextStrokePath(ctx);//开始渲染
    
    
    CGContextSetLineWidth(ctx, self.lineWidth * SDProgressViewFontScale);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGFloat to = - M_PI * 0.5 + self.progress * M_PI * 2 + 0.05; // 初始值0.05
    CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 0);
    [_progressColor setStroke];
    CGContextStrokePath(ctx);
    
    // 进度数字
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20 * SDProgressViewFontScale];
    attributes[NSForegroundColorAttributeName] = _progressColor;
    [self setCenterProgressText:_progressStr withAttributes:attributes];
}



@end
